package com.markyao.controller;

import com.markyao.service.RedisService;
import com.markyao.vo.PageParams;
import com.markyao.vo.PageVo;
import com.markyao.vo.RegistryBean;
import com.markyao.vo.Result;
import com.markyao.vo.pojo.AppBean;
import com.markyao.vo.pojo.ServiceBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@CrossOrigin
@RestController
@RequestMapping(value = "/redisServer",produces = MediaType.APPLICATION_JSON_VALUE)
public class RedisServerController {

    @Autowired
    RedisService redisService;

    @PostMapping("list")
    public Result getList(@RequestBody PageParams pageParams){
        PageVo<AppBean>pageVo=new PageVo<>();
        List<AppBean> list = redisService.getList(pageParams);

        pageVo.setList(list);
        pageVo.setTotal(pageParams.getTotal());
        return Result.success(pageVo);
    }

    @PostMapping("services")
    public Result getServices(@RequestBody PageParams pageParams){
        PageVo<ServiceBean>pageVo=new PageVo<>();
        List<ServiceBean> list = redisService.getServices(pageParams);

        pageVo.setList(list);
        pageVo.setTotal(pageParams.getTotal());
        return Result.success(pageVo);
    }

    @GetMapping("getByappName/{appName}")
    public Result getByappName(@PathVariable("appName")String appName){
        if (!StringUtils.hasText(appName))
            return Result.failed(500,"请求参数错误");
        RegistryBean registryBean=redisService.getRegistryBean(appName);
        return Result.success(registryBean);
    }

    @DeleteMapping("disconnect/{curApp}/{tarApp}")
    public Result disconnect(@PathVariable("curApp")String curApp,@PathVariable("tarApp")String tarApp){
        if (!StringUtils.hasText(curApp) ||!StringUtils.hasText(tarApp)){
            return Result.failed(500,"请求参数错误");
        }
        redisService.closeConnect(curApp,tarApp);
//        if (!f){
//            return Result.failed(500,"断开连接失败");
//        }
        return Result.success("已断开连接");
    }
    @GetMapping("reconnect/{curApp}/{tarApp}")
    public Result reconnect(@PathVariable("curApp")String curApp,@PathVariable("tarApp")String tarApp){
        if (!StringUtils.hasText(curApp) ||!StringUtils.hasText(tarApp)){
            return Result.failed(500,"请求参数错误");
        }
        redisService.reConnect(curApp,tarApp);
        return Result.success("已断开连接");
    }


}
